<?php

namespace App\Console\Gather;


use App\Service\BaiduNlp;
use App\Service\Collect\Eastmoney as Service;
use Illuminate\Console\Command;


class Eastmoney extends Base
{
    /**
     * 采集舆情文章
     *
     * 参数与选项的区别
     *      在执行命令时，参数必须严格按定义时的顺序输入，不需输入参数名，只需输入参数的值即可。
     *      输入选项则必须带选项名，且选项名前须加 -- 前缀（选项简写字母前加 -），选项可按任意顺序输入
     */

    // sudo -u www php /www/wwwroot/option/artisan command:gather-eastmoney
    //  php artisan command:gather-eastmoney
    // 请求 以及参数
    // protected $signature = 'command:name {param1} {param2}';  // 必填参数
    // protected $signature = 'command:name {参数名?} {参数名=默认值} {参数名? : 参数说明}';  // 可选参数
    // protected $signature = 'command:name {--选项名} {--选项名=} {--选项名=默认值} {--简写字母|选项名} {--选项名 : 选项说明}';  // 选项

    protected $signature   = 'command:gather-eastmoney {times=0} {number=0}';
    protected $description = '采集东方财富网';

    // 执行
    public function handle()
    {
        // 执行次数
        $times  = $this->argument('times') + 1;
        $number = $this->argument('number');
        $number = $number ?: 2000;
        $start  = ($times - 1) * $number;
        $end    = $times * $number;

        //$stocks  = $this->allStock($start, $end);
        $is_file = false;
        $stocks  = $this->allKeyword();
        $cline   = chr(10);

        // 需要采集的：搜索的
        foreach($stocks as $key => $it){
            $keyword = $it['name'];
            $page    = 1;
            $size    = 10;
            if($keyword == '宋正'){
                $keyword = '董事长宋正';
            }

            // 输出
            $this->line($cline.$cline.$cline.$cline);
            $this->question('采集股票信息：'.$keyword);
            $this->line($cline.$cline.$cline.$cline);


            // 执行采集  通过搜索采集
            for($page = 1; $page <= 10; $page++){
                // 命令窗口名称
                $this->cmdTitle($this->description."：股吧：".$it['id']."：".$keyword);

                $this->getAndSave($keyword, 'tiezi', $it['id'], $page, $size, $is_file);
                if($this->errorno == 500){
                    break;
                }
            }
            for($page = 1; $page <= 10; $page++){
                // 命令窗口名称
                $this->cmdTitle($this->description."：资讯：".$it['id']."：".$keyword);

                $this->getAndSave($keyword, 'news', $it['id'], $page, $size, $is_file);
                if($this->errorno == 500){
                    break;
                }
            }
            for($page = 1; $page <= 10; $page++){
                // 命令窗口名称
                $this->cmdTitle($this->description."：文章：".$it['id']."：".$keyword);

                $this->getAndSave($keyword, 'carticle', $it['id'], $page, $size, $is_file);
                if($this->errorno == 500){
                    break;
                }
            }
            for($page = 1; $page <= 10; $page++){
                // 命令窗口名称
                $this->cmdTitle($this->description."：公告：".$it['id']."：".$keyword);

                $this->getAndSave($keyword, 'ann', $it['id'], $page, $size, $is_file);
                if($this->errorno == 500){
                    break;
                }
            }
        }

        // 需要采集的：复旦复华股吧
        /*for($page = 1; $page <= 10; $page++){
            $keyword   = '复旦复华';
            $keywordId = 3;
            $size      = 20;
            // 命令窗口名称
            $this->cmdTitle($this->description."：股吧列表：".$keyword);

            // 通过pc端获取 要转到方正证券
            //$url = 'https://guba.eastmoney.com/list,600624_'.$page.'.html';
            //$this->getAndSaveHtml($keyword, $keywordId, $url, $is_file);

            // 通过手机端获取
            $this->getAndSaveWap($keyword, $keywordId, $page, $size, $is_file);
            if($this->errorno == 500){
                break;
            }

        }*/


        $this->showMsg('采集完成！');
    }


    // 采集东风财富网股吧：通过搜索采集
    //   $url = 'https://so.eastmoney.com/tiezi/s?keyword='.$keyword;   // 东风财富网股吧
    //   $url = https://so.eastmoney.com/tiezi/s?keyword=易见股份;   // 东风财富网股吧
    public function tiezi($keywordId)
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'tiezi', $keywordId, $page, $size);
    }


    // 采集东风财富网资讯：通过搜索采集
    //   $url = 'https://so.eastmoney.com/news/s?keyword='.$keyword;   // 东风财富网资讯
    public function news($keywordId)
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'news', $keywordId, $page, $size);
    }


    // 采集东风财富网文章：通过搜索采集
    //   $url = 'https://so.eastmoney.com/carticle/s?keyword='.$keyword;    // 东风财富网文章
    public function carticle($keywordId)
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'carticle', $keywordId, $page, $size);
    }


    // 采集东风财富网公告：通过搜索采集
    //   $url = 'https://so.eastmoney.com/ann/s?keyword='.$keyword;      // 东风财富网公告
    public function ann($keywordId)
    {
        $page    = 1;
        $size    = 10;
        $keyword = '易见股份';

        return $this->getAndSave($keyword, 'ann', $keywordId, $page, $size);
    }

    /*
     * array (
      'innerCode' => '600624',
      'shortName' => '复旦复华',
      'title' => '<em>复旦复华</em>划到农村，三农概念也没有，真是惨到家了呀[大笑]',
      'content' => '$<em>复旦复华</em>(SH600624)$<em>复旦复华</em>划到农村，三农概念也没有，真是惨到家了呀[大笑]',
      'type' => '0',
      'createTime' => '2023-12-21 10:43:36',
      'url' => 'http://guba.eastmoney.com/news,600624,1383593571.html',
      'id' => '1383593571',
    )
     */
    // 执行采集：通过搜索采集
    protected function getAndSave($keyword, $type, $keywordId, $page = 1, $size = 10, $is_file = false)
    {
        $collect = new \App\Service\Collect\Eastmoney();
        $list    = $collect->getApiList($keyword, $type, $page, $size);
        $collect->setKeyword($keyword);
        $collect->setKeywordId($keywordId);
        if(empty($list)){
            $message = $collect->getError() ?: '没有采集数据！';
            $this->showMsg($message, $collect->getCode());

            return true;
        }

        // 展示
        //var_export($list[0]);die;

        // 采集详情内容
        //   TODO 这里采集的是百度的内容
        $types = $this->allTypes();
        foreach($list as $key => $item){
            // 提示
            $this->showMsg(' '.($key + 1).'：==> 采集'.$types[$type].'：'.$item['title']);

            $res = $collect->saveApiDataToDb($item, $key, $type, $is_file);
            if($res === false){
                $this->showError($collect->getError(), $collect->getCode());
            }
        }

        return true;
    }

    // 执行采集：手机端
    protected function getAndSaveWap($keyword, $keywordId, $page = 1, $size = 10, $is_file = false)
    {
        $url     = 'https://mguba.eastmoney.com/mguba2020/interface/GetData.aspx';
        $collect = new \App\Service\Collect\Eastmoney();
        $list    = $collect->getApiWapList($url, $page, $size);
        $collect->setKeyword($keyword);
        $collect->setKeywordId($keywordId);
        if(empty($list)){
            $message = $collect->getError() ?: '没有采集数据！';
            $this->showMsg($message, $collect->getCode());

            return true;
        }

        // 整理成和上面一样的结构
        $reList = [];
        foreach($list as $_item){
            // post_type==1 只保存有资讯的
            if($_item['post_type'] == 0){
                //continue;
            }

            $reList[] = [
                'url'           => 'https://finance.eastmoney.com/a/'.$_item['post_source_id'].'.html',
                'content'       => $_item['post_content'],
                'title'         => $_item['post_title'],
                'id'            => $_item['post_source_id'],   //?
                'createTime'    => strtotime($_item['post_publish_time']),
                'author'        => $_item['post_user']['user_nickname'], //
                'innerCode'     => $_item['post_guba']['stockbar_code'], // 股票代码
                'comment_count' => $_item['source_comment_count'],       // 评论
                'like_count'    => $_item['post_like_count'],            // 点赞
                'forward_count' => $_item['post_forward_count'],         // 转发
            ];
        }

        //var_export($list[0]);
        var_export($reList);
        die;
        // 展示
        // $this->show($list[0]);

        // 采集详情内容
        //   TODO 这里采集的是百度的内容
        $types = $this->allTypes();
        foreach($list as $key => $item){
            // 提示
            $this->showMsg(' '.($key + 1).'：==> 采集'.$types[$type].'：'.$item['title']);

            $res = $collect->saveApiDataToDb($item, $key, $type, $is_file);
            if($res === false){
                $this->showError($collect->getError(), $collect->getCode());
            }
        }

        return true;
    }

    // 执行采集：通过搜索采集
    protected function getAndSaveHtml($keyword, $keywordId, $url, $is_file = false)
    {
        $collect = new \App\Service\Collect\Eastmoney();
        $list    = $collect->getHtmlList($url);

        /*$collect->setKeyword($keyword);
        $collect->setKeywordId($keywordId);
        if(empty($list)){
            $message = $collect->getError() ?: '没有采集数据！';
            $this->showMsg($message, $collect->getCode());

            return true;
        }

        // 展示
        // $this->show($list[0]);

        // 采集详情内容
        //   TODO 这里采集的是百度的内容
        $types = $this->allTypes();
        foreach($list as $key => $item){
            // 提示
            $this->showMsg(' '.($key + 1).'：==> 采集'.$types[$type].'：'.$item['title']);

            $res = $collect->saveApiDataToDb($item, $key, $type, $is_file);
            if($res === false){
                $this->showError($collect->getError(), $collect->getCode());
            }
        }*/

        return true;
    }

    // 各种分类名称：通过搜索采集
    protected function allTypes()
    {
        return ['tiezi' => '股吧', 'news' => '资讯', 'carticle' => '文章', 'ann' => '公告'];
    }

}

